package com.example.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * TTL队列
 * 设置队列 "test" 的死信队列 "dead"，采用 @Bean 的方式
 *
 * @author wuyunbin
 */
@Configuration
public class TtlQueueConfig {
    /**
     * 定义队列名称为 "test"
      */
    public static final String TEST_QUEUE_NAME = "test";
    /**
     * 定义死信队列名称为 "dead"
      */
    public static final String DEAD_QUEUE_NAME = "dead";
    /**
     * 定义交换机名称为 "exchange"
      */

    public static final String EXCHANGE_NAME = "exchange";

    @Bean
    public Queue testQueue() {
        return QueueBuilder.durable(TEST_QUEUE_NAME)
                // 设置死信队列交换机名称为空字符串，表示使用默认交换机
                .deadLetterExchange("")
                // 设置死信队列的路由键为 "dead"
                .deadLetterRoutingKey(DEAD_QUEUE_NAME)
                .build();
    }

    @Bean
    public Queue deadQueue() {
        // 创建死信队列 "dead"
        return new Queue(DEAD_QUEUE_NAME);
    }

    @Bean
    public DirectExchange exchange() {
        // 创建直连交换机 "exchange"
        return new DirectExchange(EXCHANGE_NAME);
    }

    @Bean
    public Binding binding() {
        return BindingBuilder.bind(testQueue())
                .to(exchange())
                // 将队列 "test" 绑定到交换机 "exchange"
                .with(TEST_QUEUE_NAME);
    }

}
